package merlion.crm.sopm.entity;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import merlion.common.entity.DocFile;
import merlion.common.entity.Staff;
import merlion.crm.slcm.entity.SalesLead;
import merlion.scm.lcm.entity.DeliveryOrder;

/**
 *
 * @author Zhou Hong
 */
@Entity
public class SalesQuotation implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String sqNumber;
    private String rfqNumber;
    private Timestamp quotationDate;
    private String dateStr;
    private Timestamp validityDate;
    @ManyToOne(targetEntity=SalesLead.class, cascade={CascadeType.PERSIST,CascadeType.MERGE}, fetch=FetchType.EAGER)
    private SalesLead client;
    private Boolean sentToCustomer;
    private Boolean freightOnBoard;
    private Boolean fullShipment;
    private Boolean currency;
    private String comments;
    private Double subtotal;
    private Double memberDiscount;
    private Double orderDiscount;
    private Double specialDiscount;
    private Double total;
    @OneToOne(targetEntity=DocFile.class, cascade={CascadeType.ALL}, fetch = FetchType.EAGER)
    private DocFile uploadedImage;
    @OneToMany(targetEntity=ItemLine.class, cascade={CascadeType.PERSIST,CascadeType.MERGE}, fetch=FetchType.EAGER)
    private Collection<ItemLine> itemLines;
    @OneToMany(targetEntity=DeliveryOrder.class, cascade={CascadeType.PERSIST,CascadeType.MERGE}, fetch=FetchType.EAGER)
    private Collection<DeliveryOrder> deliveryOrders;
    @ManyToOne(targetEntity=Staff.class, cascade={CascadeType.PERSIST,CascadeType.MERGE}, fetch=FetchType.EAGER)
    private Staff customerExecutive;
    private static final SimpleDateFormat formatter = new SimpleDateFormat("MMMMM dd, yyyy");
//    @OneToOne(targetEntity=MemberDiscountGroup.class, cascade={CascadeType.PERSIST,CascadeType.MERGE}, fetch=FetchType.EAGER)
//    private MemberDiscountGroup memberDiscountGroup;
    private Boolean promotion;
    //--------------------------------------------------------------------------
    //                           Constructor
    //--------------------------------------------------------------------------
    
    public SalesQuotation() {
        sqNumber = "";
        rfqNumber = "";
        quotationDate = new Timestamp(System.currentTimeMillis());
        dateStr = convertToDateFormat(quotationDate);
        client = new SalesLead();
        freightOnBoard = true;
        fullShipment  = true;
        sentToCustomer = false;
        currency = true;
        comments = "";
        total = (double)0.0;
        memberDiscount = (double)0.0;
        orderDiscount = (double)0.0;
        specialDiscount = (double)0.0;
        deliveryOrders = new ArrayList<DeliveryOrder>();
        customerExecutive = null;
        itemLines = new ArrayList<ItemLine>();
        validityDate = new Timestamp(new Date().getTime());
        long oneDay = (long)(1 * 24 * 60 * 60 * 1000);
        validityDate.setTime(quotationDate.getTime() + ((long)30 * oneDay));
        promotion = Boolean.FALSE;
    }
    
    private String convertToDateFormat(Timestamp timeStamp) {
        return formatter.format((java.util.Date) timeStamp);
    }


    //--------------------------------------------------------------------------
    //                           Getter and Setter
    //--------------------------------------------------------------------------
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public SalesLead getClient() {
        return client;
    }

    public void setClient(SalesLead client) {
        this.client = client;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public Boolean getCurrency() {
        return currency;
    }

    public void setCurrency(Boolean currency) {
        this.currency = currency;
    }

    public Staff getCustomerExecutive() {
        return customerExecutive;
    }

    public void setCustomerExecutive(Staff customerExecutive) {
        this.customerExecutive = customerExecutive;
    }

    public Timestamp getQuotationDate() {
        return quotationDate;
    }

    public void setQuotationDate(Timestamp quotationDate) {
        this.quotationDate = quotationDate;
    }

    public Timestamp getValidityDate() {
        return validityDate;
    }

    public void setValidityDate(Timestamp validityDate) {
        this.validityDate = validityDate;
    }

    public Boolean getFreightOnBoard() {
        return freightOnBoard;
    }

    public void setFreightOnBoard(Boolean freightOnBoard) {
        this.freightOnBoard = freightOnBoard;
    }
    
    public Boolean getFullShipment() {
        return fullShipment;
    }

    public void setFullShipment(Boolean fullShipment) {
        this.fullShipment = fullShipment;
    }

    public Collection<DeliveryOrder> getDeliveryOrders() {
        return deliveryOrders;
    }

    public void setDeliveryOrders(Collection<DeliveryOrder> deliveryOrders) {
        this.deliveryOrders = deliveryOrders;
    }

    public String getRfqNumber() {
        return rfqNumber;
    }

    public void setRfqNumber(String rfqNumber) {
        this.rfqNumber = rfqNumber;
    }

    public String getSqNumber() {
        return sqNumber;
    }

    public void setSqNumber(String sqNumber) {
        this.sqNumber = sqNumber;
    }

    public Double getTotal() {
        return total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    public Collection<ItemLine> getItemLines() {
        return itemLines;
    }

    public void setItemLines(Collection<ItemLine> itemLines) {
        this.itemLines = itemLines;
    }

    public Double getMemberDiscount() {
        return memberDiscount;
    }

    public void setMemberDiscount(Double memberDiscount) {
        this.memberDiscount = memberDiscount;
    }

    public Double getOrderDiscount() {
        return orderDiscount;
    }

    public void setOrderDiscount(Double orderDiscount) {
        this.orderDiscount = orderDiscount;
    }

    public Double getSpecialDiscount() {
        return specialDiscount;
    }

    public void setSpecialDiscount(Double specialDiscount) {
        this.specialDiscount = specialDiscount;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }
    
    
//
//    public MemberDiscountGroup getMemberDiscountGroup() {
//        return memberDiscountGroup;
//    }
//
//    public void setMemberDiscountGroup(MemberDiscountGroup memberDiscountGroup) {
//        this.memberDiscountGroup = memberDiscountGroup;
//    }

    public Boolean getSentToCustomer() {
        return sentToCustomer;
    }

    public void setSentToCustomer(Boolean sentToCustomer) {
        this.sentToCustomer = sentToCustomer;
    }
    
    
    
    //--------------------------------------------------------------------------
    //                          Sales Quotation Methods
    //--------------------------------------------------------------------------
    public void addDeliveryOrder(DeliveryOrder ol){
        deliveryOrders.add(ol);
    }
    
    //--------------------------------------------------------------------------
    //                               Object Methods
    //--------------------------------------------------------------------------
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof SalesQuotation)) {
            return false;
        }
        SalesQuotation other = (SalesQuotation) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.crm.slcm.entity.SalesQuotation[ id=" + id + " ]";
    }

    public Double getSubtotal() {
        return subtotal;
    }

    public void setSubtotal(Double subtotal) {
        this.subtotal = subtotal;
    }

    /**
     * @return the dateStr
     */
    public String getDateStr() {
        return dateStr;
    }

    /**
     * @param dateStr the dateStr to set
     */
    public void setDateStr(String dateStr) {
        this.dateStr = dateStr;
    }

    public DocFile getUploadedImage() {
        return uploadedImage;
    }

    public void setUploadedImage(DocFile uploadedImage) {
        this.uploadedImage = uploadedImage;
    }

    /**
     * @return the promotion
     */
    public Boolean getPromotion() {
        return promotion;
    }

    /**
     * @param promotion the promotion to set
     */
    public void setPromotion(Boolean promotion) {
        this.promotion = promotion;
    }
    
}